package com.tongji.seller.view.popup
{	
	
	import com.tongji.seller.events.ChangePropertyEvent;
	import com.tongji.seller.model.FurnitureModel;
	import com.tongji.seller.utils.PopUpAdmin;
	import com.tongji.seller.view.twoD.Furniture;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.FlexMouseEvent;
	import mx.events.Request;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 第二步编辑场景，处理点击商品时产生的titlewindow
	 * @author chenzhongyuan
	 * 2011-8-28
	 */
	
	public class ChangeFurnitureTitleWinMediator extends Mediator
	{
		[Inject]
		public var singleTw:ChangeFurnitureTitleWindow;
				
		[Inject]
		public var furnitureModel:FurnitureModel;
		private var loader:Loader;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(singleTw.product, "dataProvider", furnitureModel, "imgArray");
			eventMap.mapListener(singleTw, FlexMouseEvent.MOUSE_DOWN_OUTSIDE, removeTitleHandler, FlexMouseEvent);
			eventMap.mapListener(singleTw.cacleBtn, MouseEvent.CLICK, closeHandler, MouseEvent);
			eventMap.mapListener(singleTw.okBtn, MouseEvent.CLICK, changeImgHandler, MouseEvent);
		} 
		
		private function changeImgHandler(event:MouseEvent):void
		{ 
			if (singleTw.product.selectedIndex != -1)
			{
				loader = new Loader();
				loader.load(new URLRequest(singleTw.product.selectedItem));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
			}							
		}
		
		protected function loadCompleteHandler(event:Event):void
		{			
			furnitureModel.selectedFur.setColor(event.target.content.bitmapData);
			PopUpManager.removePopUp(singleTw);
			mediatorMap.removeMediatorByView(singleTw);
		}
		
		private function closeHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(singleTw);
			mediatorMap.removeMediatorByView(singleTw);
		}
		
		//移除titlewindow
		private function removeTitleHandler(event:FlexMouseEvent):void
		{
			PopUpManager.removePopUp(singleTw);
			mediatorMap.removeMediatorByView(singleTw);
		}
	}
}


